#include "view_time_0.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "font_mngr.h"
#include "view_common.h"
#include "app_trace.h"
#include "view_time_0.inc"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)

/**
 * @brief 左上角蓝牙图标
 *
 */
void show_time_0_ble(void)
{
#if 0
    int id = pedometer_get_gsensor_id();
#warning debug
    if (id == 0 || id == 255)
    {
        return;
    }
#endif
    if (disp_ctrl.com_data.i_temp[6])
    {
        disp_draw_user_image(3, 3 - 1, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(3, 0, img_ble_disconnect);
    }
}

/**
 * @brief 右上角电池图标
 *
 */
void show_time_0_battery(int percent, int is_charge)
{
    uint8_t battery_level = battery_ctrl_get_level(percent);
    disp_draw_user_image(212, 3, img_battery_base);
    if (is_charge)
    {
        //!< 充电中
        disp_ctrl.auto_refresh_time = 500;
        // if (battery_level == 0)
        // {
        //     battery_level = disp_ctrl.auto_refresh_count % 2;
        // }
        // else if (battery_level < 4) //充电格
        // {
        //     battery_level = disp_ctrl.auto_refresh_count % (battery_level + 1) + 1;
        // }
        battery_level = disp_ctrl.auto_refresh_count % 5;
        for (uint8_t i = 0; i < battery_level; i++)
        {
            disp_draw_user_image(214 + i * 5, 5, img_battery_each_level);
        }
    }
    else
    {
        if (battery_level == 1)
        {
            disp_draw_user_image(214, 5, img_battery_last_level);
        }
        else
        {
            for (int i = 0; i < battery_level; i++)
            {
                disp_draw_user_image(214 + i * 5, 5, img_battery_each_level);
            }
        }
    }
}
static void show_time_0_week(void)
{
    disp_draw_user_image(133 - 2, 26, img_time_0_week_TAB[disp_ctrl.com_data.i_temp[5] - 1]);
}
static void show_time_0_date(void)
{
    disp_draw_user_image(92 - 2, 25, img_time_0_date_underline);
    disp_draw_user_image(72 - 2, 25, img_time_0_date_TAB[disp_ctrl.com_data.i_temp[1] / 10]);
    disp_draw_user_image(83 - 2, 25, img_time_0_date_TAB[disp_ctrl.com_data.i_temp[1] % 10]);
    disp_draw_user_image(101 - 2, 25, img_time_0_date_TAB[disp_ctrl.com_data.i_temp[2] / 10]);
    disp_draw_user_image(112 - 2, 25, img_time_0_date_TAB[disp_ctrl.com_data.i_temp[2] % 10]);
}
static void show_time_0_heart_rate(void)
{
    //! 34 211
    disp_draw_user_image(25, 184, img_time_0_heart_rate);
    int heart_rate = disp_ctrl.com_data.i_temp[17];
    if (heart_rate < 40)
    {
        disp_draw_rect_color(24 - 1, 218, 24 - 1 + 9, 219, RGB565_COLOR_WHITE);
        disp_draw_rect_color(35 + 1, 218, 35 + 1 + 9, 219, RGB565_COLOR_WHITE);
    }
    else
    {
        disp_draw_user_image_num_mid_gray_lucency(34, 211, RGB565_COLOR_WHITE, heart_rate, 11, img_time_0_num_TAB);
    }
}
static void show_time_0_calorie(void)
{
    //! 114 211
    disp_draw_user_image(106, 182, img_time_0_calorie);
    int calorie = disp_ctrl.com_data.i_temp[16];
    disp_draw_user_image_num_mid_gray_lucency(115, 211, RGB565_COLOR_WHITE, calorie, 11, img_time_0_num_TAB);
}
static void show_time_0_steps(void)
{
    //! 195 211
    disp_draw_user_image(185, 184, img_time_0_step);
    int steps = disp_ctrl.com_data.i_temp[11];
    disp_draw_user_image_num_mid_gray_lucency(195, 211, RGB565_COLOR_WHITE, steps, 11, img_time_0_num_TAB);
}
static void show_time_0_time(void)
{
    disp_draw_user_image_filter_color(121, 90, img_time_0_colon);
    disp_draw_user_image_filter_color(1, 64, img_time_0_tens_TAB[disp_ctrl.com_data.i_temp[3] / 10]);
    disp_draw_user_image_filter_color(51, 64, img_time_0_units_TAB[disp_ctrl.com_data.i_temp[3] % 10]);
    disp_draw_user_image_filter_color(127, 64, img_time_0_tens_TAB[disp_ctrl.com_data.i_temp[4] / 10]);
    disp_draw_user_image_filter_color(177, 64, img_time_0_units_TAB[disp_ctrl.com_data.i_temp[4] % 10]);
}
void view_time_0_display(void)
{
    show_time_0_ble();
    show_time_0_battery(disp_ctrl.com_data.i_temp[7], disp_ctrl.com_data.i_temp[8]);
    show_time_0_week();
    show_time_0_date();
    show_time_0_heart_rate();
    show_time_0_calorie();
    show_time_0_steps();
    show_time_0_time();
}
